import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/clubs/create_club/create_club_controller.dart';
import 'package:flutter_changdian_football/utils/custom_toast.dart';
import 'package:flutter_changdian_football/widget/custom_button.dart';
import 'package:flutter_changdian_football/widget/dialog/club_avatar_dialog.dart';
import 'package:flutter_changdian_football/widget/dialog/featured_avatars_dialog.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/sheet/address_picker.dart';
import 'package:flutter_changdian_football/widget/sheet/param_picker.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';

/// 创建俱乐部
class CreateClubPage extends StatelessWidget {
  const CreateClubPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CreateClubController>(
      init: CreateClubController(),
      builder: (createClubController) {
        return Scaffold(
          backgroundColor: const Color(0xffF8FAF9 ),
          appBar: AppBar(
            centerTitle: true,
            elevation: 0,
            title: Text(
              '创建俱乐部',
              style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
            ),
            iconTheme: const IconThemeData(
              color: Color(0xff333333)
            ),
            backgroundColor: const Color(0xffF8FAF9 ),
          ),
          bottomNavigationBar: Container(
            padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 8.w),
            width: double.infinity,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                CustomButton(
                    text: '创建俱乐部',
                    onTap: () {
                      createClubController.addClub();
                    },
                  )
              ],
            ),
          ),
          body: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            padding: EdgeInsets.symmetric(horizontal: 17.w),
            child: SizedBox(
              width: double.infinity,
              child: Column(
                children: [
                  SizedBox(height: 20.w,),
            
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      ClubAvatarDialog.show(
                        Get.context!,
                        isRecommend: true,
                        proposeTap: () {
                          Get.back();
                          FeaturedAvatarsDialog.show(
                            Get.context!,
                            defaultClubAvatarList: createClubController.defaultClubAvatarList,
                            confirmTap: (v) {
                              createClubController.clubImage = v;
                              createClubController.update();
                              Get.back();
                            },
                          );
                        },
                        phoneTap: () {
                          createClubController.photoSelectImage();
                        },
                        shootTap: () {
                          createClubController.shootSelectImage();
                        },
                      );
                    },
                    child: Container(
                      width: 88.w,
                      height: 88.w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12.w),
                        color: const Color(0xffA25353).withOpacity(0.1)
                      ),
                      padding: EdgeInsets.all(4.w),
                      child: ClipRRect(
                          borderRadius: BorderRadius.circular(12.r),
                        child: createClubController.clubImage == null ? Image.asset('assets/images/logo_gr.png', fit: BoxFit.fill,) :
                        Image.network(createClubController.clubImage ?? '', fit: BoxFit.fill,)
                      ),
                    ),
                  ),
            
                  SizedBox(height: 12.w,),
            
                  SizedBox(
                    child: Text('点击设置俱乐部徽章', style: TextStyle(fontSize: 15.sp, color: const Color(0xff323232)),),
                  ),

                  SizedBox(height: 20.w,),


                  Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12.r),
                      color: Colors.white
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 12.w),
                    child: Column(
                      children: [
                        // _leftAndRightWidget(leftText: '俱乐部名称', rightText: ''),
                        SizedBox(
                          child: Row(
                            children: [
                              SizedBox(
                                child: Text('俱乐部名称', style: TextStyle(fontSize: 15.sp, color: const Color(0xff323232)),),
                              ),

                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left: 15.w),
                                  child: Column(
                                    children: [
                                      TextField(
                                        keyboardType: TextInputType.text,
                                        maxLines: 1,
                                        maxLength: 12,
                                        textAlign: TextAlign.end,
                                        controller: createClubController.clubNameController,
                                        decoration: InputDecoration(
                                          contentPadding: EdgeInsets.symmetric(vertical: 0.w),
                                          border: const OutlineInputBorder(
                                            borderSide: BorderSide.none,
                                          ),
                                          hintStyle: TextStyle(fontSize: 14.sp, color: const Color(0xff999999), height: 1.5),
                                          hintText: '请输入俱乐部名称',
                                          counterText: '',
                                        ),
                                        style: TextStyle(fontSize: 14.sp, color: const Color(0xff9E9E9E), height: 1.5),
                                        onChanged: (v) {
                                        },
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),

                        Container(
                          width: double.infinity,
                          height: 1.w,
                          color: const Color(0xffF8FAF9),
                        ),
                        _leftAndRightWidget(leftText: '俱乐部所在地', rightText: createClubController.clubAddress ?? '请选择俱乐部所在地', onTap: () {
                          AddressPicker.show(
                            context,
                            initProvince: createClubController.province1 ?? '',
                            initCity: createClubController.city1 ?? '',
                            onConfirm: (province, city, district) {
                              createClubController.changeAddress(province, city, district);
                            }
                          );
                        }),
                        Container(
                          width: double.infinity,
                          height: 1.w,
                          color: const Color(0xffF8FAF9),
                        ),
                        _leftAndRightWidget(leftText: '活动主场', rightText: createClubController.clubHomeCourtName ?? '请选择活动主场', onTap: () {
                          if (createClubController.clubAddress == null) {
                            CustomToast.showTextToast('请先选择俱乐部所在地');
                            return ;
                          }
                          ParamPicker.show(
                            '活动主场', 
                            context, 
                            createClubController.courtStrList,
                            selecteds: createClubController.selectCourtIndex,
                            onConfirm: (v) {
                              createClubController.changeSelectCourtIndex(v);
                            }
                          );
                        }),
                        Container(
                          width: double.infinity,
                          height: 1.w,
                          color: const Color(0xffF8FAF9),
                        ),
                        _leftAndRightWidget(leftText: '竞技强度', rightText: createClubController.selectJjqdIndex == -1 ? '请选择竞技强度' : createClubController.jjqdStrList[createClubController.selectJjqdIndex],
                        onTap: () {
                          ParamPicker.show(
                            '竞技强度', 
                            context, 
                            createClubController.jjqdStrList,
                            selecteds: createClubController.selectJjqdIndex,
                            onConfirm: (v) {
                              createClubController.changeSelectJjqdIndex(v);
                            }
                          );
                        })
                      ],
                    ),
                  ),


                  SizedBox(height: 10.w,),

                  Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(12.r),
                    ),
                    height: 140.w,
                    child: Column(
                      children: [
                        Expanded(
                          child: TextField(
                            keyboardType: TextInputType.text,
                            maxLines: 10,
                            controller: createClubController.clubProfileController,
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.symmetric(vertical: 14.w, horizontal: 12.w),
                              border: const OutlineInputBorder(
                                borderSide: BorderSide.none,
                              ),
                              hintStyle: TextStyle(fontSize: 15.sp, color: const Color(0xff9E9E9E)),
                              hintText: '俱乐部简介',
                              counterText: '',
                            ),
                            style: TextStyle(fontSize: 15.sp, color: const Color(0xff9E9E9E)),
                            onChanged: (v) {
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    );
  }


  _leftAndRightWidget({
    String leftText = '',
    String rightText = '',
    bool isRight = true,
    Function? onTap
  }) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        onTap?.call();
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 15.w),
        child: Row(
          children: [
            SizedBox(
              child: Text(leftText, style: TextStyle(fontSize: 15.sp, color: const Color(0xff323232)),),
            ),
    
            const Spacer(),
    
            SizedBox(
              child: Text(rightText, style: TextStyle(fontSize: 14.sp, color: const Color(0xff999999), height: 1.5),),
            ),
    
            if (isRight) ...[
              SizedBox(width: 8.w,),
    
              Container(
                margin: EdgeInsets.only(top: 1.w),
                width: 10.w,
                height: 10.w,
                child: Image.asset('assets/images/icon_arrow_grey_right.png'),
              )
            ],
          ],
        ),
      ),
    );
  }

}